/*
 * @Description: 
 * @Project: 
 * @Author: michelle
 * @Date: 2022-02-24 10:42:11
 * @LastEditors: michelle
 * @LastEditTime: 2022-02-24 13:19:04
 * @Modified By: michelle
 * @FilePath: /trunkverse/src/components/private-route/index.jsx
 */
import React from 'react'
import { useLocation, Navigate } from 'react-router-dom'
import CONFIG from '../../../config'

const o = Object.create(null);

export default function PrivateRoute({
    element: Component,
    meta = o,
    ...rest
}) {

    React.useEffect(() => {
        if (meta.title) {
            document.title = `${meta.title} -${CONFIG.title}`
        } else {
            document.title = CONFIG.title
        }
    }, [meta])

    const { pathname, search } = useLocation();
    //  const { isLogin } = useAppSelector(state => state.user)
    //是否登陆标识
    const isLogin = true; //window.localStorage.getItem('token') ? true : false;
    const isLoginPage = pathname === '/' || pathname === '/login'
    if (isLoginPage && isLogin) {
        return <Navigate to='/home/index' replace />
    }

    if (meta.requiresAuth) {
        if (isLogin) {
            return <Component {...rest} />
        } else {
            if (!isLoginPage) {
                return <Navigate to={`/?redirectUrl=${pathname}${search}`} replace />
            }
        }
    }

    return <Component {...rest} />
}
